
using System;
using System.Text;
using NuevoGenNHibernate.CEN.Ebay;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Criterion;
using NHibernate.Exceptions;
using NuevoGenNHibernate.EN.Ebay;
using NuevoGenNHibernate.Exceptions;

namespace NuevoGenNHibernate.CAD.Ebay
{
public partial class LineaVentaCAD : BasicCAD, ILineaVentaCAD
{
public LineaVentaCAD() : base ()
{
}

public LineaVentaCAD(ISession sessionAux) : base (sessionAux)
{
}



public LineaVentaEN ReadOIDDefault (int id_lineaVenta)
{
        LineaVentaEN lineaVentaEN = null;

        try
        {
                SessionInitializeTransaction ();
                lineaVentaEN = (LineaVentaEN)session.Get (typeof(LineaVentaEN), id_lineaVenta);
                SessionCommit ();
        }

        catch (Exception ex) {
                SessionRollBack ();
                if (ex is NuevoGenNHibernate.Exceptions.ModelException)
                        throw ex;
                throw new NuevoGenNHibernate.Exceptions.DataLayerException ("Error in LineaVentaCAD.", ex);
        }


        finally
        {
                SessionClose ();
        }

        return lineaVentaEN;
}


public int CrearLineaVenta (LineaVentaEN lineaVenta)
{
        try
        {
                SessionInitializeTransaction ();
                if (lineaVenta.CompraloYA != null) {
                        lineaVenta.CompraloYA = (NuevoGenNHibernate.EN.Ebay.VentaEN)session.Load (typeof(NuevoGenNHibernate.EN.Ebay.VentaEN), lineaVenta.CompraloYA.Id_venta);

                        lineaVenta.CompraloYA.LineaVenta.Add (lineaVenta);
                }
                if (lineaVenta.Articulos != null) {
                        lineaVenta.Articulos = (NuevoGenNHibernate.EN.Ebay.ArticuloEN)session.Load (typeof(NuevoGenNHibernate.EN.Ebay.ArticuloEN), lineaVenta.Articulos.Id_articulo);

                        lineaVenta.Articulos.LineasVenta.Add (lineaVenta);
                }

                session.Save (lineaVenta);
                SessionCommit ();
        }

        catch (Exception ex) {
                SessionRollBack ();
                if (ex is NuevoGenNHibernate.Exceptions.ModelException)
                        throw ex;
                throw new NuevoGenNHibernate.Exceptions.DataLayerException ("Error in LineaVentaCAD.", ex);
        }


        finally
        {
                SessionClose ();
        }

        return lineaVenta.Id_lineaVenta;
}

public void ModificarLineaVenta (LineaVentaEN lineaVenta)
{
        try
        {
                SessionInitializeTransaction ();
                LineaVentaEN lineaVentaEN = (LineaVentaEN)session.Load (typeof(LineaVentaEN), lineaVenta.Id_lineaVenta);

                lineaVentaEN.Numero = lineaVenta.Numero;


                lineaVentaEN.Cantidad = lineaVenta.Cantidad;

                session.Update (lineaVentaEN);
                SessionCommit ();
        }

        catch (Exception ex) {
                SessionRollBack ();
                if (ex is NuevoGenNHibernate.Exceptions.ModelException)
                        throw ex;
                throw new NuevoGenNHibernate.Exceptions.DataLayerException ("Error in LineaVentaCAD.", ex);
        }


        finally
        {
                SessionClose ();
        }
}
public void EliminarLineaVenta (int id_lineaVenta)
{
        try
        {
                SessionInitializeTransaction ();
                LineaVentaEN lineaVentaEN = (LineaVentaEN)session.Load (typeof(LineaVentaEN), id_lineaVenta);
                session.Delete (lineaVentaEN);
                SessionCommit ();
        }

        catch (Exception ex) {
                SessionRollBack ();
                if (ex is NuevoGenNHibernate.Exceptions.ModelException)
                        throw ex;
                throw new NuevoGenNHibernate.Exceptions.DataLayerException ("Error in LineaVentaCAD.", ex);
        }


        finally
        {
                SessionClose ();
        }
}

public LineaVentaEN ReadOID (int id_lineaVenta)
{
        LineaVentaEN lineaVentaEN = null;

        try
        {
                SessionInitializeTransaction ();
                lineaVentaEN = (LineaVentaEN)session.Get (typeof(LineaVentaEN), id_lineaVenta);
                SessionCommit ();
        }

        catch (Exception ex) {
                SessionRollBack ();
                if (ex is NuevoGenNHibernate.Exceptions.ModelException)
                        throw ex;
                throw new NuevoGenNHibernate.Exceptions.DataLayerException ("Error in LineaVentaCAD.", ex);
        }


        finally
        {
                SessionClose ();
        }

        return lineaVentaEN;
}

public System.Collections.Generic.IList<LineaVentaEN> ReadAll (int first, int size)
{
        System.Collections.Generic.IList<LineaVentaEN> result = null;
        try
        {
                SessionInitializeTransaction ();
                if (size > 0)
                        result = session.CreateCriteria (typeof(LineaVentaEN)).
                                 SetFirstResult (first).SetMaxResults (size).List<LineaVentaEN>();
                else
                        result = session.CreateCriteria (typeof(LineaVentaEN)).List<LineaVentaEN>();
                SessionCommit ();
        }

        catch (Exception ex) {
                SessionRollBack ();
                if (ex is NuevoGenNHibernate.Exceptions.ModelException)
                        throw ex;
                throw new NuevoGenNHibernate.Exceptions.DataLayerException ("Error in LineaVentaCAD.", ex);
        }


        finally
        {
                SessionClose ();
        }

        return result;
}
}
}
